﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	internal class CombinedSectionOperate
	{
		public CombinedSectionOperate(Autodesk.Revit.DB.Document doc, FrmCommbinedPipline frm)
		{
			this.doc = doc;
			this.uiDoc = new UIDocument(doc);
			this.frm = frm;
		}

		public void Execute()
		{
			List<Element> pipelineInProject = this.GetPipelineInProject();
			this.GetDeleteTagMEPCruves(pipelineInProject);
			this.GetDeletElems(pipelineInProject);
			this.DeletNum(pipelineInProject);
			this.DeletElems();
		}

		private List<Element> GetPipelineInProject()
		{
			List<Element> result;
			if (this.frm.CombinedMethod)
			{
				result = (from elem in new FilteredElementCollector(this.doc, this.doc.ActiveView.Id).OfClass(typeof(MEPCurve)).ToList<Element>()
				where (this.doc.GetElement(elem.GetTypeId()) as Autodesk.Revit.DB.ElementType).Name == this.frm.ElemType.Name
				select elem).ToList<Element>();
			}
			else
			{
				result = this.uiDoc.Selection.PickElementsByRectangle(new DuctPipeCableTrayConduitFilter(), "请框选需要合并的管线...").ToList<Element>();
			}
			return result;
		}

		private void GetDeleteTagMEPCruves(List<Element> elems)
		{
			foreach (Element element in elems)
			{
				this.selectedPipelinesIdIntegerValue.Add(element.Id.IntegerValue);
			}
		}

		public void DeletNum(List<Element> elems)
		{
			foreach (Element element in ((IEnumerable<Element>)new FilteredElementCollector(this.doc).OfClass(typeof(IndependentTag)).ToList<Element>()))
			{
				IndependentTag independentTag = element as IndependentTag;
				ElementId taggedLocalElementId = independentTag.TaggedLocalElementId;
				if (this.selectedPipelinesIdIntegerValue.Contains(taggedLocalElementId.IntegerValue))
				{
					this.doc.Delete(independentTag.Id);
					Parameter parameter = this.doc.GetElement(taggedLocalElementId).GetParameters("分段编号").FirstOrDefault<Parameter>();
					if (parameter != null && !parameter.IsReadOnly)
					{
						parameter.Set("");
					}
				}
			}
		}

		private void GetDeletElems(List<Element> elems)
		{
			foreach (Element element in elems)
			{
				MEPCurve mepcurve = element as MEPCurve;
				if (!this.deletElemIds.Contains(mepcurve.Id.IntegerValue))
				{
					ConnectorSetIterator connectorSetIterator = mepcurve.ConnectorManager.Connectors.ForwardIterator();
					while (connectorSetIterator.MoveNext())
					{
						object obj = connectorSetIterator.Current;
						Connector connector = obj as Connector;
						if (connector.IsConnected)
						{
							ConnectorSetIterator connectorSetIterator2 = connector.AllRefs.ForwardIterator();
							while (connectorSetIterator2.MoveNext())
							{
								object obj2 = connectorSetIterator2.Current;
								Element element2 = (obj2 as Connector).Owner as FamilyInstance;
								if (element2 is FamilyInstance)
								{
									FamilyInstance familyInstance = element2 as FamilyInstance;
									MechanicalFitting mechanicalFitting = familyInstance.MEPModel as MechanicalFitting;
									if (mechanicalFitting != null && (int)mechanicalFitting.PartType == 13)
									{
										if (!this.frm.CombinedMethod)
										{
											ConnectorSetIterator connectorSetIterator3 = familyInstance.MEPModel.ConnectorManager.Connectors.ForwardIterator();
											while (connectorSetIterator3.MoveNext())
											{
												object obj3 = connectorSetIterator3.Current;
												ConnectorSetIterator connectorSetIterator4 = (obj3 as Connector).AllRefs.ForwardIterator();
												while (connectorSetIterator4.MoveNext())
												{
													object obj4 = connectorSetIterator4.Current;
													Element element3 = (obj4 as Connector).Owner as MEPCurve;
													if (element3 != null && !this.selectedPipelinesIdIntegerValue.Contains(element3.Id.IntegerValue))
													{
														this.selectedPipelinesIdIntegerValue.Add(element3.Id.IntegerValue);
													}
												}
											}
										}
										this.deletElemIds.Add(familyInstance.Id.IntegerValue);
									}
								}
							}
						}
					}
				}
			}
		}

		private void DeletElems()
		{
			foreach (int num in this.deletElemIds)
			{
				ElementId elementId = new ElementId(num);
				if (this.doc.GetElement(elementId) != null)
				{
					this.doc.Delete(elementId);
				}
			}
		}

		private List<string> GetDeletFamiyName()
		{
			return new List<string>
			{
				"矩形法兰",
				"矩形活接头",
				"椭圆形活接头",
				"圆形活接头",
				"活接头-卡箍",
				"活接头-承插",
				"活接头-法兰",
				"活接头-钢活套",
				"活接头-焊缝",
				"活接头-热熔",
				"活接头-丝接",
				"活接头-粘接",
				"槽式电缆桥架活接头",
				"梯级式电缆桥架活接头",
				"托盘式电缆桥架活接头",
				"线管接头 - PVC",
				"线管接线盒 - 活接头 - PVC"
			};
		}

		private Document doc;

		private UIDocument uiDoc;

		private List<MEPCurve> linesForNum = new List<MEPCurve>();

		private FrmCommbinedPipline frm;

		private List<int> deletElemIds = new List<int>();

		private List<int> selectedPipelinesIdIntegerValue = new List<int>();
	}
}
